/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utilities;

import java.util.Calendar;
import javax.swing.JComboBox;

/**
 *
 * @author VietLH
 */
public class DateValidate {
    private int maxYear = Calendar.getInstance().get(Calendar.YEAR);
    private int maxMonth = 11;
    private int maxDay = 31;
    private SimpleDateFormatter sdf;
    private String[] monthList = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    private String date;
    private JComboBox cbDay=new JComboBox();
    private JComboBox cbMonth=new JComboBox();
    private JComboBox cbYear=new JComboBox();

    public DateValidate(JComboBox cbDay, JComboBox cbMonth, JComboBox cbYear) {
        this.cbDay = cbDay;
        this.cbMonth = cbMonth;
        this.cbYear = cbYear;
        initDate();
    }

    public DateValidate() {
    }
      
    
    private void initDate() {
        initDay(maxDay);
        initMonth(maxMonth);
        initYear(maxYear);
    }

    private void initDay(int maxDay) {
        cbDay.removeAllItems();
        for (int day = 1; day <= maxDay; day++) {
            cbDay.addItem(day);
        }
    }

    private void initMonth(int maxMonth) {
        cbMonth.removeAllItems();        
        for (int month = 0; month <= maxMonth; month++) {
            cbMonth.addItem(monthList[month]);
        }
    }

    private void initYear(int maxYear) {
        cbYear.removeAllItems();
        for (int year = 1900; year <= maxYear; year++) {
            cbYear.addItem(year);
        }
    }

    public void checkValidMonth() {
        int selectedDay = cbDay.getSelectedIndex();
        int selectedMonth = cbMonth.getSelectedIndex();
        int selectedYear = cbYear.getSelectedIndex();
        if (cbYear.getSelectedIndex() == (maxYear - 1900)) {
            if (cbMonth.getSelectedIndex() > Calendar.getInstance().get(Calendar.MONTH)) {

                initDay(maxDay);
                initMonth(Calendar.getInstance().get(Calendar.MONTH));
                cbDay.setSelectedIndex(selectedDay);
            } else if ((cbMonth.getSelectedIndex() == Calendar.getInstance().get(Calendar.MONTH))
                    && (cbDay.getSelectedIndex() + 1 > Calendar.getInstance().get(Calendar.DAY_OF_MONTH))) {
                initDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
            } else {
                if ((cbYear.getSelectedIndex() % 4 == 0) && (cbMonth.getSelectedIndex() + 1 == 2)) {
                    initDay(29);
                    if (selectedDay < 29) {
                        cbDay.setSelectedIndex(selectedDay);
                    }
                } else if ((cbMonth.getSelectedIndex() + 1 == 4) || (cbMonth.getSelectedIndex() + 1 == 6)
                        || (cbMonth.getSelectedIndex() + 1 == 9) || (cbMonth.getSelectedIndex() + 1 == 11)) {
                    initDay(30);
                    if (selectedDay < 30) {
                        cbDay.setSelectedIndex(selectedDay);
                    }
                } else if (cbMonth.getSelectedIndex() + 1 == 2) {
                    initDay(28);
                    if (selectedDay < 28) {
                        cbDay.setSelectedIndex(selectedDay);
                    }
                } else {
                    initDay(maxDay);
                    cbDay.setSelectedIndex(selectedDay);
                }
            }

        } else if ((cbMonth.getSelectedIndex() + 1 == 4) || (cbMonth.getSelectedIndex() + 1 == 6)
                || (cbMonth.getSelectedIndex() + 1 == 9) || (cbMonth.getSelectedIndex() + 1 == 11)) {
            initDay(30);
            if (selectedDay < 30) {
                cbDay.setSelectedIndex(selectedDay);
            }
        } else if (cbMonth.getSelectedIndex() + 1 == 2) {
            initDay(28);
            if (selectedDay < 28) {
                cbDay.setSelectedIndex(selectedDay);
            }
        } else {
            initDay(maxDay);
            cbDay.setSelectedIndex(selectedDay);
        }
        if ((cbYear.getSelectedIndex() % 4 == 0) && (cbMonth.getSelectedIndex() + 1 == 2)) {
            if (cbMonth.getSelectedIndex() + 1 == 2) {
                initDay(29);
                if (selectedDay < 29) {
                    cbDay.setSelectedIndex(selectedDay);
                }
            }
        }
    }

    public void checkValidDay() {
        if ((cbYear.getSelectedIndex() == (maxYear - 1900))
                && (cbMonth.getSelectedIndex() == Calendar.getInstance().get(Calendar.MONTH))
                && (cbDay.getSelectedIndex() + 1 > Calendar.getInstance().get(Calendar.DAY_OF_MONTH))) {
            initDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
        }
    }

    public void changeExistYear() {
        int selectedDay = cbDay.getSelectedIndex();
        int selectedMonth = cbMonth.getSelectedIndex();
        initDay(maxDay);
        initMonth(maxMonth);
        cbDay.setSelectedIndex(selectedDay);
        cbMonth.setSelectedIndex(selectedMonth);
    }
}
